package br.com.hstar.client.cliente.tipocliente;

import br.com.hstar.client.ServiceFactory;
import br.com.hstar.client.util.FormPanel;
import br.com.hstar.client.util.HstarDialogBox;
import br.com.hstar.client.util.HstarUserException;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class TipoClienteEditPanel extends FormPanel {

	private Button incluir;
	private Button cancelar;
	private Button excluir;
	
	private TextBox descricao;
	private TipoClienteUI tipoClienteUI;

	public TipoClienteEditPanel(String _title) {
		super(_title);
		tipoClienteUI = new TipoClienteUI();
		init(true);
	}
	
	public TipoClienteEditPanel(String _title, Long id) {
		super(_title);
		tipoClienteUI = new TipoClienteUI();
		tipoClienteUI.setId(id);
		init(false);
		ServiceFactory.createTipoClienteService().retrieveById(id, new RetrieveAsyncCallback());
	}
	
	
	
	//Methods -------------------------------------------------------------------
	
	private void init(boolean isNew) {

		descricao = addTextBox("Tipo do Cliente", 40, 50, "");
		
		if(isNew) {
			incluir  = addButton("Incluir");
			cancelar = addButton("Cancelar");
		} else {
			incluir  = addButton("Alterar");
			cancelar = addButton("Cancelar");
			excluir = addButton("Excluir");
			excluir.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					ServiceFactory.createTipoClienteService().delete(tipoClienteUI.getId(), new DeleteAsyncCallback());
				}
			});
		}
		
		incluir.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				 try {
					copyData();
					ServiceFactory.createTipoClienteService().saveOrUpdate(tipoClienteUI, new SaveAsyncCallback());
				} catch (HstarUserException e) {
					new HstarDialogBox("Tipo do Cliente", e.getMessage());					
				}
				 
			}
		});
		cancelar.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				RootPanel.get("boxContent").clear();
				RootPanel.get("boxContent").add(new TipoClienteListPanel("Tipo do Cliente"));
			}
		});
		
	}

	private void copyData() throws HstarUserException {
		if(descricao.getText().trim() == "")
		{
			throw new HstarUserException(" - Tipo do Cliente deve ser preenchido. <br>");			
		}
			
		tipoClienteUI.setDescricao(descricao.getText().trim());
	}

	// INNER CLASSES ----------------------------------------------------------------
	private class SaveAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			if(caught instanceof HstarUserException)
			{					
				new HstarDialogBox("Tipo do cliente",caught.getMessage());
			}
		}

		public void onSuccess(Object result) {
			RootPanel.get("boxContent").clear();
			RootPanel.get("boxContent").add(new TipoClienteListPanel("Tipo do Cliente"));
		}
	}
	
	private class DeleteAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			Window.alert("ERRO - Tipo do Cliente");
		}

		public void onSuccess(Object result) {
			RootPanel.get("boxContent").clear();
			RootPanel.get("boxContent").add(new TipoClienteListPanel("Tipo do Cliente"));
		}
	}
	
	//carrega os campos. Usado para quando está editando
	private class RetrieveAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			Window.alert("ERRO - Tipo do Cliente");
		}

		public void onSuccess(Object result) {
			TipoClienteUI tipoCliente = (TipoClienteUI)result;

			descricao.setText(tipoCliente.getDescricao().trim());
		}
	}
}      